package com.bite.book.controller;

import com.bite.book.model.*;
import com.bite.book.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2025-05-07
 * Time:22:05
 */
@Slf4j//打印日志
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired//可通过其去BookService中去拿对象
    private BookService bookService;

    /*添加图书*/
    @RequestMapping(value = "/addBook",produces = "application/json")
    public String addBook(BookInfo bookInfo) {
        //校验参数
        log.info("添加图书，接收到参数：bookInfo {}", bookInfo);
        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus() == null) {
            return "输入参数不合法！";
        }
        //添加图书
        try {
            Integer result = bookService.insertBook(bookInfo);
            if (result > 0) {
                return "";
            }
        } catch (Exception e) {
            log.error("添加图书异常，e: ", e);
        }
        return "添加失败";
    }

    /*查询图书列表（接口)*/
    @RequestMapping("/getBookListByPage")
    public Result<PageResult<BookInfo>> getBookListByPage(PageRequest pageRequest, HttpSession session) {
        log.info("查询图书列表，请求参数pageRequest: {}", pageRequest);
//        //从session获取用户信息
//        //如果用户信息为空，说明用户未登录
//        UserInfo loginUserInfo = (UserInfo) session.getAttribute(Constants.USER_SESSION_KEY);
//        if(loginUserInfo==null||loginUserInfo.getId()<0){
//            return Result.nologin();
//        }
        PageResult<BookInfo> bookList = bookService.getBookListByPage(pageRequest);
        return Result.success(bookList);
    }

    /*获取图书信息*/
    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId) {
        log.info("根据id查询图书信息，id:" + bookId);
        long start=System.currentTimeMillis();
        BookInfo bookInfo = bookService.queryBookById(bookId);
        log.info("queryBookById 耗时："+(System.currentTimeMillis()-start)+"ms");
        return bookInfo;
    }

    /*更新图书*/
    @RequestMapping(value = "/updateBook",produces = "application/json")
    public String updateBook(BookInfo bookInfo) {
        log.info("更新图书，bookInfo： {}", bookInfo);//调用的是对象
        try {
            Integer result = bookService.updateBookById(bookInfo);
            if (result > 0) {
                return "";
            }
            return "内部错误，请联系管理员";
        } catch (Exception e) {
            log.error("更新图书失败，e:", e);
            return "内部错误，请联系管理员";
        }
    }

    /*删除图书--用的是逻辑删除，update,delete是物理删除，删除的是status*/
    @RequestMapping("/deleteBook")
    public Boolean deleteBook(Integer bookId) {
        log.info("删除图书，bookId： {}",bookId);//调用的是对象
        Integer result = bookService.deleteBook(bookId);
        if (result > 0) {
            return true;
        }
        return false;
    }

    //批量删除
    @RequestMapping(value = "/batchDeleteBook",produces = "application/json")
    public String batchDelete(@RequestParam List<Integer> ids){//加上@RequestParam注解，才能把参数绑定给list
        log.info("批量删除图书，ids： {}",ids);
        Integer result=bookService.batchDeleteBookByIds(ids);
        if (result>0){
            return "";
        }
        return "删除失败，请联系管理员";
    }
}
